//
//  RBLogisticsVC.m
//  zhongzhenglu
//
//  Created by 马军凯 on 2023/5/8.
//

#import "RBLogisticsVC.h"
#import "RBLogisticsResponse.h" //model
#import "RBLogisticsHeaderView.h" //头
#import "RBLogisticsListCell.h" //cell
#import "RBLogisticsFooterView.h" //尾

@interface RBLogisticsVC ()
//tableView
@property (nonatomic, strong) BaseTableView *tableView;
//
@property (nonatomic, strong) RBLogisticsHeaderView *headView;
//
@property (nonatomic, strong) RBLogisticsResponse *response;
//
@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation RBLogisticsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
    
    [self getData];
}
//
- (void)setup {
    self.title = @"查看物流";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
    
    //
    LCWeakSelf;
    self.tableView.CreateCell = ^UITableViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath) {
        RBLogisticsListCell *cell = [RBLogisticsListCell baseCellWith:weakSelf.tableView];
        //model
        RBLogisticsKuaidiDataModel *model = weakSelf.dataArray[indexPath.row];
        if (model) {
            cell.model = model;
        }
        
        return cell;
    };
}



#pragma mark 网络请求
- (void)getData {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"order_id"] = self.order_id;
    } path:@"han_kuaidi" post:YES log:YES responseClass:RBLogisticsResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBLogisticsResponse *data) {
        weakSelf.response = data;
        [weakSelf fillData];
    } failBlock:^(RBLogisticsResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:^{
    }];
}
//
- (void)fillData {
    [self.dataArray removeAllObjects];
    NSArray *data = self.response.data.kuaidi.data;
    for (int i = 0; i < data.count; i++) {
        RBLogisticsKuaidiDataModel *model = data[i];
        model.index = i;
        model.isLast = NO;
        if (i == (data.count - 1)) {
            model.isLast = YES;
        }
        [self.dataArray addObject:model];
    }
    //列表
    self.tableView.rows = @[@(self.dataArray.count)];
    [self.tableView reloadData];
    //头赋值
    self.headView.model = self.response.data;
}


#pragma mark 懒加载
- (BaseTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[BaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.bounces = NO;
        _tableView.sections = 1;
        _tableView.backgroundColor = self.view.backgroundColor;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.mas_equalTo(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
        //头
        self.headView = kRBGetViewFromNib(@"RBLogisticsHeaderView");
        self.tableView.tableHeaderView = self.headView;
        //尾
        RBLogisticsFooterView *footView = kRBGetViewFromNib(@"RBLogisticsFooterView");
        self.tableView.tableFooterView = footView;
    }
    return _tableView;
}
#pragma mark 懒加载
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
